package com.example.book_shop_java.util;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    private static String BASE_PATH = "E:\\Users\\hzy\\Desktop\\book_shop\\book_shop\\book_shop_java\\src\\main\\resources\\static\\upload";

    private static final String SERVER_PATH = "http://localhost:8888/upload/";

    public static String upload(MultipartFile file){
        String filename = file.getOriginalFilename();

        String uuid = UUID.randomUUID().toString().replace("-","");

        String newFileName = uuid+"-"+filename;
        File image = new File(BASE_PATH,newFileName);

        try{
            file.transferTo(image);
        } catch (IOException e){
            e.printStackTrace();
        }

        return SERVER_PATH + newFileName;
    }
}
